var url = "";
var count = -1;
var catObjRemove = new Array();
//Setup Menu

function setupMenu() {
	document.getElementById("menuId").innerHTML = '<input type="button" value="User & Group" onclick="javascript:funSubmit(' + '\'editUser_Group\'' + ')" />'
												+ '<input type="button" value="Save All" onclick="javascript:saveAll()" />'
												+ '<input type="button" value="Rules" onclick="javascript:funSubmit(' + '\'display\'' + ')" />';
}

function editGroup(groupName, groupId) {
	  document.adminForm.valueGroupName.value = groupName;
	  document.getElementById('groupId').value = groupId;  	  
}


function deleteGroup(groupName, groupId) {
	var answer = confirm ("Ban muon xoa Group: " + groupName + "?" );
	if (answer) {
		document.getElementById('groupId').value = groupId;  
		funSubmit ("deleteGroup");//Submit
	}
}  

function deleteUserFromGroup(idUser, groupId) {	
	
	//DELETE User by Group
	url = "index.php?option=com_accessrule&task=deleteUserFromGroup&idUser="+idUser;
	ajaxRes (url, "");//nothing to display
	
	//Query Again
	getUsersByGroup(groupId);
}

/**
* Add User into Group
*/
function addUserIntoGroup(idUser) {
	//ADD User into Group
	url = "index.php?option=com_accessrule&task=addUserIntoGroup&idUser=" + idUser + "&groupName=" + document.getElementById('groupNameId').value + "&groupId=" + document.getElementById('groupId').value;
	alert (url);
	ajaxRes (url, "");//nothing to display
	
	//Query Again
	getUsersByGroup(document.getElementById('groupId').value, document.getElementById('groupNameId').value);

}

function getUsersByGroup(groupId, groupName) {	
	//Assign variable
	document.getElementById('groupId').value = groupId;
	document.getElementById('groupNameId').value = groupName;

	//TRUE means All of Users being in Group
	url = "index.php?option=com_accessrule&task=getUsersByGroup&groupIdName="+groupId +"&status=TRUE";
	
	ajaxRes (url, "userOfGroup");

	//FALSE means All of Users NOT being in Group
	url = "index.php?option=com_accessrule&task=getUsersByGroup&groupIdName="+groupId +"&status=FALSE";
	ajaxRes (url, "userNotBeInGroup");
}

function loopSelected()	{
  var textIdSection = "";
  var selectedArray = new Array();
  var secObj = document.getElementById('idSections');
  var catObj = document.getElementById('idCategories');
  var i;
  for (i=0; i<secObj.options.length; i++) {
	if (secObj.options[i].selected) {
	  textIdSection = secObj.options[i].value;
	  //selectedArray[count] = selObj.options[i].value;
	  break;
	}
  }
  
  var length = catObj.options.length;	  
  for (i=0; i<count; i++) {
	catObj.options[length + i] = catObjRemove[i];
  }
  count = -1;
  
  //alert (count);
  
  
  for (i=0; i<catObj.options.length; i++) {
	if (catObj.options[i].id != textIdSection) {
	  //catObj.options[i].style.display = 'none';				  
	  count++;
	  catObjRemove[count] = catObj.options[i];
	  catObj.remove(i);
	  i=-1;
	} else {
		//catObj.options[i].style.display = 'block';				  
		//var newOpt1 = new Option("newText", "newValue");
		//catObj.options[0] = newOpt1;
		//catObj.options[i].selected = true;
	}
  }	  
  
 //txtSelectedValuesObj.value = "TUYEN";
}

function funSubmit (strName) {
	var taskObj = document.getElementById('idTask');
	taskObj.value = strName;

	document.adminForm.submit();
}

function getUserType () {
	url = "index.php?option=com_accessrule&task=searchUserType&userTypeName="+document.getElementById('userType').value;
	ajaxRes (url, "userTypeArea");
}

function getGroups () {
	url = "index.php?option=com_accessrule&task=getGroups";
	ajaxRes (url, "userTypeArea");
}


function searchContents () {
	url = "index.php?option=com_accessrule&task=searchContents";
	url = url + "&idSections="+document.getElementById('idSections').value;
	url = url + "&idCategories="+document.getElementById('idCategories').value;	
	if (document.getElementById('idTypeofSerachContent_CONTENT').checked) {
		url = url + "&typeofSerachContent="+document.getElementById('idTypeofSerachContent_CONTENT').value;				
	} else {
		url = url + "&typeofSerachContent="+document.getElementById('idTypeofSerachContent_CATEGORY').value;	
	}
		
	ajaxRes (url, "contentArea");
}
	
function saveAll () {
	var rules = getRules();
	if (rules != "") {
		//GET Id of UserType to give Array		
		var arrUserTypeCheckbox = getArraytoGETmethod (document.adminForm.userTypeCheckbox);//get by Name
		
		//GET Id of Contents to give Array
		var arrContentsCheckbox;
		var contentTypeName;
		if (document.getElementById('idTypeofSerachContent_CATEGORY').checked) {
			//arrContentsCheckbox = getArraytoGETmethod ((document.adminForm.categoryCheckbox))
			contentTypeName = "CATEGORY";
		} else {				
			contentTypeName = "CONTENT";
		}
		
		arrContentsCheckbox = getArraytoGETmethod (document.adminForm.contentCheckbox);					
		url = "index.php?option=com_accessrule&task=saveAll";
		url = url + "&arrUserTypeCheckbox=" + arrUserTypeCheckbox;
		url = url + "&arrContentsCheckbox=" + arrContentsCheckbox;	
		url = url + "&userTypeName=" + document.getElementById('userType').value;	
		url = url + "&contentTypeName=" + contentTypeName;
		url = url + "&rules=" + rules;		
		
		ajaxRes (url, "message");
	} else {
		alert ("HAY CHON QUYEN");
	}
}

function getArraytoGETmethod (checkbox) {
	var arrArray;
	var intLength = checkbox.length;
	if (intLength > 0) {
		var i;
		var count = 0;
		arrArray = new Array();
		for (i = 0; i< checkbox.length; i++) {
			if(checkbox[i].checked) {
				arrArray[count] = checkbox[i].value;
				count++;
			}
		}
	}
	return arrArray;
}

function ajaxRes(url, divHTML){	 
	var ajaxRequest;	   
	try{
	  // Opera 8.0+, Firefox, Safari
	  ajaxRequest = new XMLHttpRequest();
	} catch (e){
	  // Internet Explorer Browsers
	  try{
		 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
		 try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		 } catch (e){
			// Something went wrong
			alert("Incompatible Browser");
			var ajaxDisplay = document.getElementById('ajaxDiv');
			statusDisplay.innerHTML = "Incompatible Browser";
			return false;
		 }
	  }
   }

   /// Create a function that will receive data sent from the server
   ajaxRequest.onreadystatechange = function(){
	   if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){
			// For some reason, the returned text has a bunch of carrage returns which messes things up unless removed with trim.
			 var ajaxResponse = trim(ajaxRequest.responseText);
			 if(ajaxResponse=='') {
				if (typeof document.adminForm.onsubmit == "function") {
				   document.adminForm.onsubmit();
				}
				document.adminForm.submit();
			 } else {
				document.getElementById(divHTML).innerHTML = ajaxResponse;
			 }
		  }
   }

	//url = "index.php?option=com_accessrule&task=searchUserType&userTypeName="+document.getElementById('userType').value;
	//alert (url);
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null);
}

// **************************************************************
function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i > 0; i--) {
	  if (/\S/.test(str.charAt(i))) {
		 str = str.substring(0, i + 1);
		 break;
	  }
	}
	return str;
}

function getRules () {
	var rule = "";
	if (document.getElementById('idEdit').checked) {
		rule = rule + "Edit" + " ";
	}
	if (document.getElementById('idDelete').checked) {
		rule = rule + "Delete" + " "
	}
	if (document.getElementById('idPublic').checked) {
		rule = rule + "Public" + " "
	}		
	return (trim(rule));
}

function changeTypeofSerachContent () {
	if (document.getElementById('idTypeofSerachContent_CONTENT').checked) {			
		document.getElementById('idCategories').style.display = 'block';	
		searchContents();//get Content again
					  
	} else if (document.getElementById('idTypeofSerachContent_CATEGORY').checked) {
		document.getElementById('idCategories').style.display = 'none';	
		searchContents();
	}
}